package day8;

import java.io.*;
import java.net.Socket;

/**
 * @author oldliu
 * @since 1.0
 */
//聊天线程
public class ChatThread extends Thread {
    private final Socket s;

    public ChatThread(Socket s) {
        this.s = s;
    }

    @Override
    public void run() {
        try {
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            PrintWriter pw = new PrintWriter(out, true);//
            //聊天，直到碰到quit就停
            String words = null;
            //回复 知道了
            while (true) {
                words = br.readLine();

                if (words.length() == 0)
                    continue;
                System.out.println("客户端说：" + words);
                pw.println("你说的问题：" + words + ",朕知道了");
                //pw.flush();
                if ("quit".equals(words))
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                s.close();
            } catch (Exception e) {

            }
        }
    }
}
